//
//  YKEmotionTabBar.m
//  youkan
//
//  Created by 吴高烽 on 16/4/14.
//  Copyright © 2016年 Honsintech. All rights reserved.
//  表情键盘底部选项卡

#import "YKEmotionTabBar.h"
#import "YKEmotionTabBarButton.h"
@interface YKEmotionTabBar()
@property (nonatomic ,strong)YKEmotionTabBarButton *currentButton;
@end
@implementation YKEmotionTabBar

-(instancetype)initWithFrame:(CGRect)frame{
    if (self =[super initWithFrame:frame]) {
        [self setUpAllChilen];
    }
    return self;
}
-(void)setUpAllChilen{
    [self setupButton:@"最近" buttonType:YKEmotionTabBarTypeReccent];
    [self setupButton:@"默认"buttonType:YKEmotionTabBarTypeDefault];
    [self setupButton:@"Emoji" buttonType:YKEmotionTabBarTypeEmoji];
    [self setupButton:@"浪小花" buttonType:YKEmotionTabBarTypeLXH];
}
-(void)setupButton:(NSString*)title buttonType:(YKEmotionTabBarType)buttonType{
    YKEmotionTabBarButton *btn =[YKEmotionTabBarButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:title forState:UIControlStateNormal];
    btn.tag =buttonType;
    //    设置背景颜色
    [btn setBackgroundImage:[UIImage imageNamed:@"compose_emotion_table_left_normal"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchDown];
    [btn setBackgroundImage:[UIImage imageNamed:@"compose_emotion_table_left_selected"] forState:UIControlStateDisabled];
    [self addSubview:btn];
 
}
-(void)setDelegate:(id<YKEmotionTabBarDelegate>)delegate{
        _delegate =delegate;
//    设置默认
        [self btnclick:[self viewWithTag:YKEmotionTabBarTypeDefault]];
}
-(void)btnclick:(YKEmotionTabBarButton*)btn{
    self.currentButton.enabled =YES;
    btn.enabled =NO;
    self.currentButton =btn;
//    通知代理
    if ([_delegate respondsToSelector:@selector(emtionTabBar:didSelectedButton:)]) {
        [_delegate emtionTabBar:self didSelectedButton:btn.tag];
    }
}
-(void)layoutSubviews{
    [super layoutSubviews];
    NSUInteger count =self.subviews.count;
    CGFloat btnx =0;
    CGFloat btny=0;
    CGFloat btnw =self.width/count;
    CGFloat btnh =self.height;
    for (int i=0; i<count; i++) {
        btnx =i*btnw;
        UIButton *btn =self.subviews[i];
        btn.frame =CGRectMake(btnx, btny, btnw, btnh);
    }
}
@end
